package model

import (
	"fmt"
	"choujiang/common/db"
)

type UserGoodsLog struct {
	Id         int64  `xorm:"INT(11) not null pk autoincr" json:"id"`
	Nickname   string `xorm:"VARCHAR(100)" json:"nickname"`
	HeadImgUrl string `xorm:"VARCHAR(255)" json:"head_img_url"`
	UserId     int64  `xorm:"INT(11) not null default 0" json:"user_id"`
	GoodsId    int64  `xorm:"INT(11) not null default 0" json:"goods_id"`
	CouponNum  int64  `xorm:"INT(11) not null default 0" json:"coupon_num"`
	CreatedAt  int64  `xorm:"not null default 0 INT(11) created" json:"created_at"`
	UpdatedAt  int64  `xorm:"not null default 0 INT(11) updated" json:"updated_at"`
}

func (u *UserGoodsLog) Insert() (*UserGoodsLog, error) {
	_, err := db.MysqlEngine.InsertOne(u)
	return u, err
}

func (u *UserGoodsLog) Update() (*UserGoodsLog, error) {
	_, err := db.MysqlEngine.ID(u.Id).Update(u)
	return u, err
}

func GetUserGoodsLog(userId, goodsId int64) *UserGoodsLog {
	user := new(UserGoodsLog)
	has, err := db.MysqlEngine.Where("user_id=? and goods_id=?", userId, goodsId).Get(user)
	if !has || err != nil {
		fmt.Printf("GetUserByUid err %+v\n", err)
		return nil
	}
	return user
}
